package org.rapla.mobile.android.service;

import org.rapla.facade.ClientFacade;
import org.rapla.framework.RaplaException;
import org.rapla.mobile.android.RaplaMobileException;
import org.rapla.mobile.android.utility.FacadeFactory;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

/**
 * Facade Service
 * 
 * The facade service class runs as a background process and exposes
 * the Rapla client facade to the actual android application by
 * creating only one instance.
 * 
 * The intent to start this service must provide the following parameters:
 * * String url			- Url of rapla server
 * * int port			- Port of rapla server
 * * String username	- Rapla system username
 * * String password	- Password of user
 * 
 * @author Maximilian Lenkeit <dev@lenki.com>
 */
public class FacadeService extends Service {

	private ClientFacade facade;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		String url = intent.getExtras().getString("url");
		int port = intent.getExtras().getInt("port");
		String username = intent.getExtras().getString("username");
		String password = intent.getExtras().getString("password");

		try {
			this.facade = FacadeFactory.getInstance().createInstance(url, port);
			this.login(this.facade, username, password);
		} catch (RaplaMobileException rme) {
			// TODO What to do with the exception?
		}

		return super.onStartCommand(intent, flags, startId);
	}

	/**
	 * Log user on to the Rapla backend system via client facade
	 * @param facade Client facade (parameterized to avoid dependency injections)
	 * @param username Rapla system username
	 * @param password Password of user
	 * @throws RaplaMobileException
	 */
	private void login(ClientFacade facade, String username, String password)
			throws RaplaMobileException {
		try {
			// Try to login to facade with given user credentials
			if (!facade.login(username, password.toCharArray())) {
				// Login failed, throw exception
				throw new RaplaMobileException("Login failed");
			}
		} catch (RaplaException re) {
			// Some Rapla exception was thrown, just pass an
			throw new RaplaMobileException(re);
		}
	}

	/**
	 * Get facade
	 * @return ClientFacade
	 */
	public ClientFacade getFacade() {
		return this.facade;
	}

	/**
	 * Facade Service Binder
	 * 
	 * The facade service binder class is an internal helper class for enabling
	 * activities to access the service and consume its API
	 * 
	 * @author Maximilian Lenkeit <dev@lenki.com>
	 */
	public class FacadeServiceBinder extends Binder {

		/**
		 * Get running facade service instance
		 * @return FacadeService The currently running facade service instance
		 */
		public FacadeService getService() {
			return FacadeService.this;
		}
	}

}
